<template>
  <div :class="{'hidden':hidden}"
       class="pagination-container">
    <el-pagination :background="background"
                   :current-page.sync="currentPage"
                   :page-size.sync="pageSize"
                   :layout="layout"
                   :page-sizes="pageSizes"
                   :total="total"
                   v-bind="$attrs"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange" />
  </div>
</template>

<script>
// import {scrollTo} from '@/utils/scroll-to'

export default {
  name: 'Pagination',
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 10
    },
    pageSizes: {
      type: Array,
      default () {
        return [10, 15, 20, 30]
      }
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    }
  },
  data: function () {
    return {
      myPage: null,
      myLimit: null
    }
  },
  computed: {
    currentPage: {
      get () {
        return this.myPage
      },
      set (val) {
        this.myPage = val
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get () {
        return this.myLimit
      },
      set (val) {
        this.myLimit = val
        this.$emit('update:limit', val)
      }
    }
  },
  created () {
    this.initPagination()
  },
  methods: {
    initPagination () {
      this.$emit('pagination', { pageNum: this.currentPage, pageSize: this.pageSize })

    },
    handleSizeChange (val) {
      this.pageSize = val
      this.$emit('pagination', { pageNum: this.currentPage, pageSize: val })
      // if (this.autoScroll) {
      //   scrollTo(0, 800)
      // }
    },
    handleCurrentChange (val) {
      this.currentPage = val
      this.$emit('pagination', { pageNum: val, pageSize: this.pageSize })
    }
  },
  watch: {
    page: {
      handler: function (val) {
        this.myPage = val
      },
      immediate: true
    },
    limit: {
      handler: function (val) {
        this.myLimit = val
      },
      immediate: true
    }
  }
}
</script>

<style lang="scss">
.pagination-container {
  padding: 32px 16px;
  height: auto !important;
  display: flex;
  justify-content: flex-end;

  .el-pagination {
    position: relative !important;
  }
}

.pagination-container.hidden {
  display: none;
}
</style>
